{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```py\n",
    "\n",
    "def protocol_log(func):\n",
    "    async def warpper(*args, **kwargs):\n",
    "        try:\n",
    "            result = await func(*args, **kwargs)\n",
    "            logger.info(f\"method {func.__name__} was called\")\n",
    "            return result\n",
    "        except Exception as e:\n",
    "            logger.error(f\"method {func.__name__} raised a error: {e.__class__.__name__}\")\n",
    "            raise e\n",
    "    return warpper\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```py\n",
    "import asyncio\n",
    "import types\n",
    "\n",
    "\n",
    "def say_hello(function):\n",
    "    def decorated(*args, **kwargs):\n",
    "        function_instance = function(*args, **kwargs)\n",
    "        if isinstance(function_instance, types.AsyncGeneratorType):\n",
    "            async def inner():\n",
    "                print(\"Hello async generator!\")\n",
    "                async for v in function_instance:\n",
    "                    yield v\n",
    "        else:\n",
    "            async def inner():\n",
    "                print(\"Hello coroutine!\")\n",
    "                return await function_instance\n",
    "        return inner()\n",
    "\n",
    "    return decorated\n",
    "\n",
    "\n",
    "@say_hello\n",
    "async def generator():\n",
    "    for i in range(5):\n",
    "        await asyncio.sleep(0.2)\n",
    "        yield i\n",
    "\n",
    "\n",
    "@say_hello\n",
    "async def coroutine():\n",
    "    await asyncio.sleep(1)\n",
    "    return list(range(5))\n",
    "\n",
    "\n",
    "async def test():\n",
    "    async for v in generator():\n",
    "        print(v)\n",
    "    print(await coroutine())\n",
    "\n",
    "\n",
    "asyncio.run(test())\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
